<template>
  <div class='container'>
    <el-card class="box-card">

      <el-form class="demo-form-inline" label-width="80px">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-form-item label="目录名称">
              <el-input v-model="queryParams.directoryName" placeholder="请输入" style="width:250px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="状态">
              <el-select v-model="formInline.state" placeholder="请选择">
                <el-option v-for="item,index in status" :key="index" :label="item.label" :value="item.value"></el-option>
              </el-select>
            </el-form-item>

          </el-col>
          <el-col :span="6">
            <el-form-item>
              <el-button type="success" @click="handleOpen"><i class="el-icon-edit"></i>新增目录</el-button>

            </el-form-item>
          </el-col>

        </el-row>
        <el-form-item>
          <el-button @click="queryParams.directoryName=''">清除</el-button>
          <el-button type="primary" @click="loadList" v-model="queryParams.keyword">查询</el-button>
        </el-form-item>
      </el-form>
      <el-alert type="info">
        <i class="el-icon-info"></i>
        数据一共{{total}}条
      </el-alert>
      <el-table style="width: 100%" :data="list">
        <el-table-column type="index" label="序号" width="100">
        </el-table-column>
        <el-table-column prop="subjectName" label="所属学科">
        </el-table-column>
        <el-table-column prop="directoryName" label="目录名称">
        </el-table-column>
        <el-table-column prop="username" label="创建者">
        </el-table-column>
        <el-table-column label="创建日期">
          <template v-slot='{row}'>
            {{$dateFormat(row.addDate)}}
          </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试题数量">
        </el-table-column>
        <el-table-column prop="state" label="状态" :formatter="formType">
        </el-table-column>
        <el-table-column prop="address" label="操作">
          <template v-slot="{row}">
            <el-button type="text">
              <span @click="cut(row)">{{row.state===1?'禁用':'启用'}}</span>
            </el-button>
            <el-button type="text" :disabled="row.state === 0? false:true">修改</el-button>
            <el-button type="text" :disabled="row.state === 0? false:true" @click=" handleClick(row)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
      <el-dialog title="新增目录" width='500px'>
        <el-form label-width="100px" class="demo-ruleForm">
          <el-form-item label="所属学科" prop="subjectID">
            <el-select placeholder="请选择">
              <el-option></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" prop="directoryName">
            <el-input placeholder="请选择目录名称"></el-input>
          </el-form-item>
        </el-form>
        <div class="right">
          <template>
            <el-button>取消</el-button>
            <el-button type="primary">确定</el-button>
          </template>
        </div>
      </el-dialog>
      <el-pagination @current-change="handleCurrentChange" :current-page="queryParams.page" :page-size="queryParams.pagesize" layout="total, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <DirectorysAdd :show-dialog.sync="showDialog" />
  </div>
</template>

<script>
import { list, remove, changeState } from '../../api/hmmm/directorys'
import DirectorysAdd from '../components/directorys-add.vue'
import { status } from '@/api/hmmm/constants.js'
export default {
  data () {
    return {
      status: status,
      statusx: null,
      // name: 'directorys',
      showDialog: false,
      formInline: {
        directoryName: '',
        state: ''

      },
      queryParams: {
        page: 1,
        pagesize: 10,
        directoryName: '',
        statusx: null,
        keyword: ''
      },
      tableData: [],
      total: 0,
      showAuthDialog: false,
      catalogueForm: {
        subjectID: '',
        directoryName: ''
      },
      subjectName: [],
      rules: {
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'change' }
        ],
        directoryName: [
          { required: true, message: '请输入目录名称', trigger: 'blur' },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      list: [],
      simple: []
    }
  },
  created () {
    this.loadList()
  },
  components: { DirectorysAdd },
  methods: {
    // 转换状态
    formType (row) {
      const statusx = [
        {
          value: 1,
          label: '已启用'
        },
        {
          value: 0,
          label: '已禁用'
        }
      ]
      const info = statusx.find(item => parseInt(item.value) === parseInt(row.state))
      if (info) {
        return info.label
      } else {
        return '其他'
      }
    },
    // 启用禁用
    async cut (row) {
      if (row.state === 1) {
        row.state = 0
        const res = await changeState({
          id: row.id,
          state: row.state
        })
        this.statusx = res.data.success
      } else if (row.state === 0) {
        row.state = 1
        const res = await changeState({
          id: row.id,
          state: row.state
        })
        this.statusx = res.data.success
      }
    },
    // 分页
    handleCurrentChange (page) {
      this.queryParams.page = page
      this.loadList()
    },
    // 删除
    async handleClick (id) {
      try {
        const ret = await remove(id)
        if (ret.status === 200) {
          this.loadList()
          this.$message.success('删除成功')
        }
      } catch (err) {
        this.$message.error('删除失败')
      }
    },
    // 获取列表
    async loadList () {
      try {
        const ret = await list(this.queryParams)
        this.list = ret.data.items
        this.total = ret.data.counts
      } catch (e) {
        this.$message.error('获取失败')
        console.log(e)
      }
    },
    // 删除
    handleOpen () {
      this.showDialog = true
    }

    // ,
    // async loadList () {
    //   try {
    //     const ret = await list(this.queryParams)
    //     console.log(ret)
    //     this.list = ret.data.items
    //   } catch (e) {
    //     this.$message.error('获取失败')
    //     console.log(e)
    //   }
    // }
  }
}

</script>
<style scoped>
.demo-form {
  display: flex;
  /* justify-content: space-around; */
}
</style>
